package lbframework.bean.mdiFrame;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.classe.Util;
import lbframework.remote.Arquivo;

public class LBMdiFrame extends JFrame {

    public JDesktopPane desktopPane = null;
    public Vector<LBInternalFrame> vJanela = new Vector();
    private JMenu mnuJanela = null;

    public LBMdiFrame() throws Exception {
        initComponents();
    }

    public void setIcon(String i_resource) throws Exception {
        this.setIconImage(new javax.swing.ImageIcon(getClass().getResource(i_resource)).getImage());
    }

    public void atualizarRodape() throws Exception {
    }

    public void atualizarJanela() throws Exception {
        if (mnuJanela == null) {
            return;
        }

        //verifica janela selecionada
        LBInternalFrame oInternalFrame = null;

        for (int i = 0; i < this.desktopPane.getComponentCount(); i++) {
            Component oComponente = this.desktopPane.getComponent(i);

            if (oComponente instanceof LBInternalFrame) {
                oInternalFrame = (LBInternalFrame) oComponente;

                if (oInternalFrame.isSelected()) {
                    break;
                }
            }
        }

        //recria menu janela
        mnuJanela.removeAll();

        for (int i = 0; i < vJanela.size(); i++) {
            LBInternalFrame oJanela = vJanela.get(i);

            JCheckBoxMenuItem mnuItem = new JCheckBoxMenuItem(oJanela.getTitle());
            mnuJanela.add(mnuItem);

            if (oJanela.equals(oInternalFrame)) {
                mnuItem.setSelected(true);
            }

            mnuItem.setActionCommand(String.valueOf(i));

            mnuItem.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        int i = Integer.parseInt(e.getActionCommand());
                        vJanela.get(i).setSelected(true);

                        ((JCheckBoxMenuItem) e.getSource()).setSelected(true);

                    } catch (Exception ex) {
                    }
                }
            });
        }

        //resetar janela
        if (vJanela.size() > 0) {
            mnuJanela.add(new JSeparator());
        }

        JMenuItem mnuResetarJanela = new JMenuItem("Resetar Janelas");
        mnuJanela.add(mnuResetarJanela);

        mnuResetarJanela.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    Util.exibirMensagemContinuar("Todas as janelas voltarão ao seu formato padrão!", getTitle());

                    Arquivo arq = new Arquivo(Util.getRoot() + Util.FILE_LOG_GUI, "w");
                    arq.close();

                    Util.exibirMensagem("Resetado com sucesso!", getTitle());

                } catch (Exception ex) {
                }
            }
        });
    }

    public void setMnuJanela(JMenu i_mnuJanela) {
        this.mnuJanela = i_mnuJanela;
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
